package com.erp.pur.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.erp.common.utils.*;
import com.erp.itm.domain.ProjectDO;
import com.erp.itm.service.ProjectService;
import com.erp.pro.domain.InstockDO;
import com.erp.pro.domain.SupplierDO;
import com.erp.pro.service.SupplierService;
import com.erp.pur.domain.PaymentDO;
import com.erp.system.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.pur.domain.GroupDO;
import com.erp.pur.service.GroupService;

import javax.servlet.http.HttpServletResponse;

/**
 * 采购主表
 * 
 * @author Eric
 * @email shiliang900713@163.com
 * @date 2019-11-09 09:57:14
 */
 
@Controller
@RequestMapping("/pur/group")
public class GroupController {
	@Autowired
	private GroupService groupService;

	@Autowired
	private ProjectService projectService;

	@Autowired
	private SupplierService supplierService;
	
	@GetMapping()
	String Group(){
	    return "pur/group/group";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<GroupDO> groupList = groupService.list(query);
		int total = groupService.count(query);
		PageUtils pageUtils = new PageUtils(groupList, total);
		return pageUtils;
	}

	@ResponseBody
	@GetMapping("/getlistByPayment")
	public List<GroupDO> getlistByPayment(){
		List<GroupDO> groupList = groupService.list(null);
		return groupList;
	}

	
	@GetMapping("/add")
	String add(){
	    return "pur/group/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Long id,Model model){
		getPurByid(id, model);
		return "pur/group/edit";
	}
	@GetMapping("/getinfo/{id}")
	String getinfo(@PathVariable("id") Long id,Model model){
		getPurByid(id, model);
		return "pur/group/getinfo";
	}

	private void getPurByid(@PathVariable("id") Long id, Model model) {
		GroupDO group = groupService.get(id);
		if(group.getAllPurprice() == null){
			group.setAllPurprice(new BigDecimal("0"));
		}
		if(group.getPaymentPrice() == null){
			group.setPaymentPrice(new BigDecimal("0"));
		}
		group.setNopaymentPrice(group.getAllPurprice().subtract(group.getPaymentPrice()));
		if(group.getInvoicePrice() == null){
			group.setInvoicePrice(0D);
		}
		group.setNoinvoicePrice(group.getAllPurprice().doubleValue()-group.getInvoicePrice());
		model.addAttribute("group", group);
		//查询项目信息
		List<ProjectDO> projectDOList = projectService.getNotEndProjectlist();
		model.addAttribute("projectDOList", projectDOList);
		//查询供应商信息
		List<SupplierDO> supplierList = supplierService.list(null);
		model.addAttribute("supplierList", supplierList);
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( GroupDO group){
		group.setStatus(1);
		group.setPaymentPrice(new BigDecimal("0"));
		group.setInvoicePrice(0D);
		initInfo(group,"add");
		if(groupService.save(group)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( GroupDO group){
		initInfo(group,"update");
		groupService.update(group);
		return R.ok();
	}

	/**
	 * 初始化
	 * @param group
	 * @param operate
	 */
	private void initInfo(GroupDO group,String operate) {
		//查询项目信息
		if(group.getProjectId() != null){
			ProjectDO projectDO = projectService.get(group.getProjectId());
			if(projectDO != null){
				group.setProjectName(projectDO.getItmNumber());
			}
		}
		//查询供应商
		if(group.getSupplierId() != null){
			SupplierDO supplierDO = supplierService.get(group.getSupplierId());
			if(supplierDO != null){
				group.setSupplierName(supplierDO.getName());
			}
		}
		UserDO userDO = ShiroUtils.getUser();
		if("add".equals(operate)){
			group.setGmtCreate(new Date());
			group.setUserIdCreate(userDO.getUserId());
			group.setUserNameCreate(userDO.getName());
			group.setContractUserid(userDO.getUserId());
			group.setContractUsername(userDO.getName());
		}else{
			group.setContractUserid(userDO.getUserId());
			group.setContractUsername(userDO.getName());
		}
	}


	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Long id){
		if(groupService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}


	/**
	 * 确认
	 */
	@PostMapping( "/confirm")
	@ResponseBody
	public R confirm( Long id){
		if(groupService.confirm(id)>0){
			return R.ok();
		}
		return R.error();
	}

	@GetMapping("/getsupplier/{supplierId}")
	String getsuppliertab(@PathVariable("supplierId") Long supplierId,Model model){
		model.addAttribute("supplierId",supplierId);
		model.addAttribute("firstsrc","/pro/supplier/getsupplierinfo/"+supplierId);
		return "pur/group/getsuppliertab";
	}

	@GetMapping("/getsupplierList/{supplierId}")
	String edittab(@PathVariable("supplierId") Long supplierId, Model model){
		model.addAttribute("supplierId",supplierId);
		return "pur/group/suppliergrouplist";
	}

	@ResponseBody
	@GetMapping("/listBysupplierId")
	public PageUtils listBysupplierId(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<GroupDO> groupList = groupService.list(query);
		int total = groupService.count(query);
		PageUtils pageUtils = new PageUtils(groupList, total);
		return pageUtils;
	}

	/**
	 * 查询采购总价
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getgroup/{id}")
	public PaymentDO getgroup(@PathVariable("id") Long id){
		//查询列表数据
		GroupDO groupDO = groupService.get(id);
		//查询供应商对应的信息
		SupplierDO supplierDO = supplierService.get(groupDO.getSupplierId());
		PaymentDO paymentDO = new PaymentDO();
		paymentDO.setId(groupDO.getId());
		paymentDO.setGroupid(groupDO.getGroupid());
		paymentDO.setSupplierId(groupDO.getSupplierId());
		paymentDO.setSupplierName(supplierDO.getName());
		paymentDO.setAllPurprice(groupDO.getAllPurprice());
		paymentDO.setPaymentPrice(groupDO.getPaymentPrice());
		paymentDO.setOpenBank(supplierDO.getOpenBank());
		paymentDO.setBankAccount(supplierDO.getBankAccount());
		return paymentDO;
	}

	@PostMapping("/exportData")
	@ResponseBody
	public void downloadTemplate(GroupDO groupDO, HttpServletResponse response){
		//ProjectDO project
		Map<String, Object> params = new HashMap<>();
		if(StringUtils.isNotBlank(groupDO.getSupplierName())){
			params.put("supplierName",groupDO.getSupplierName());
		}
		if(StringUtils.isNotBlank(groupDO.getGroupid())){
			params.put("groupid",groupDO.getGroupid());
		}
		if(groupDO.getPurType() != null){
			params.put("purType",groupDO.getPurType());
		}
		List<GroupDO> groupDOList = groupService.list(params);
		for (GroupDO obj:groupDOList) {
			switch (obj.getStatus()){
				case 1:
					obj.setStatusname("新创建");
					break;
				case 4:
					obj.setStatusname("采购确认");
					break;
				case 5:
					obj.setStatusname("已删除");
					break;
				default:
					obj.setStatusname("新创建");
			}
			switch (obj.getPurType()){
				case 1:
					obj.setPurTypeName("公司采购");
					break;
				case 2:
					obj.setPurTypeName("现场采购");
					break;
				case 3:
					obj.setPurTypeName("分包");
					break;
				case 4:
					obj.setPurTypeName("其他");
					break;
				default:
					obj.setStatusname("公司采购");
			}
		}
		ExcelUtil.exportExcel(groupDOList,"采购列表","采购列表",GroupDO.class,"采购列表.xls",response);
	}


	@ResponseBody
	@GetMapping("/getGroup/{id}")
	public  GroupDO getInfo(@PathVariable("id")Long id){
		GroupDO groupDO =  groupService.get(id);
		return groupDO;
	}
}
